package Quartz;

import java.util.ArrayList;
import java.util.List;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import Candidate.CandidateManager;
import Eclass.Exam.ExamManager;

public class QuartzClosed implements Job {
	public void execute(JobExecutionContext context)
			throws JobExecutionException {
		
		double score;
		QuartzManager schedulerManager = new QuartzManager();
		QuartzModel schedModel=new QuartzModel();
		ExamManager exManager=new ExamManager();
		CandidateManager cManager=new CandidateManager();
		String[] open = context.getJobDetail().getKey().getName().toString()
				.split("[+]");
		
		if(open[0].equals("ReloadCandidate")){
			System.out.println(open[0]+" "+open[1]+" "+open[2]);
			score=exManager.calculateScore(open[1], Integer.parseInt(open[2]));
			if(score!=0.0){
				score = Math.round(score * 100.0) / 100.0;
				}
			exManager.insertExamScore(0, score, open[1], "Candidate");
			System.out.println("SCORE FOR RELOAD CANDIDATE"+score);
			
		}else if(open[0].equalsIgnoreCase("ResetRegNo")){
			cManager.resetRegNoSeq();
		}
		else{
		schedModel=null;
		schedModel=schedulerManager.selectExamFor(open[1]);
		if("Trainee".equalsIgnoreCase(schedModel.getExamFor())){
			System.out.println(schedulerManager.updateOpen(open[1], "0"));
			List result = new ArrayList();
			result = schedulerManager.selectUserScore(open[1]);
			if(result.size()!=0){
				for (int x = 0; x < result.size(); x++) {
					QuartzModel schedulerModel = (QuartzModel) result.get(x);
					System.out.println("-------USERID AFTER UJIAN"+schedulerModel.getUserId());
					
					score=exManager.calculateScore(schedulerModel.getUserId(), Integer.parseInt(open[1]));
					if(score!=0.0){
					score = Math.round(score * 100.0) / 100.0;
					}
					System.out.println(schedulerModel.getUserId()+"-"+schedulerModel.getBatchClassId()+"-"+score);
					exManager.insertExamScore(Integer.parseInt(schedulerModel.getBatchClassId()), score, schedulerModel.getUserId(), "Trainee");
				
					if(x==(result.size()-1)){
						schedulerManager.countGrade(Integer.parseInt(schedulerModel.getBatchClassId()));
					}
				}
			}
			
		}else if("Candidate".equalsIgnoreCase(schedModel.getExamFor())){
			System.out.println("SCORE FOR CANDIDATE");
			List result = new ArrayList();
			result = schedulerManager.selectUserScoreCandidate(open[1]);
			if(result.size()!=0){
				for (int x = 0; x < result.size(); x++) {
					QuartzModel schedulerModel = (QuartzModel) result.get(x);
					System.out.println(schedulerModel.getUserId());
					score=exManager.calculateScore(schedulerModel.getUserId(), Integer.parseInt(open[1]));
					if(score!=0.0){
					score = Math.round(score * 100.0) / 100.0;
					}
					exManager.insertExamScore(0, score, schedulerModel.getUserId(), "Candidate");
				}
			}
		}
		
		}
		
		System.out.println("EXAM IS CLOSED,------------------------- ~");
		System.out.println("Job name: "
				+ context.getJobDetail().getKey().getName());
		System.out.println("Trigger name: "
				+ context.getTrigger().getKey().getName());
		System.out.println("Firing Time: " + context.getFireTime());
	}
}